package com.security.door.controller.areaManage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.crAreaDevice;
import com.security.door.model.dto.crAreaDeviceDto;
import com.security.door.model.vo.crAreaDeviceVo;
import com.security.door.service.IcrAreaDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - crAreaController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class crAreaDeviceController {
    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcrAreaDeviceService icrAreaDeviceService;

    @PostMapping("/areaDevice/queryListWithPage")
    @ApiOperation(value = "分页查询所有区域信息", notes = "分页查询所有区域信息")
    public Wrapper<PageInfo<crAreaDeviceVo>> queryListWithPage(@RequestBody crAreaDeviceDto entityDto) {
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        PageHelper.startPage(pageIndex, pageSize);
        var list = icrAreaDeviceService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/areaDevice/del/{guid}")
    @ApiOperation(value = "分页查询所有区域信息", notes = "分页查询所有区域信息")
    public Wrapper delAreaDevice(@PathVariable("guid") String guid) {
        icrAreaDeviceService.deleteByKey(guid);
        return WrapMapper.ok();
    }

    @PostMapping("/areaDevice/grant")
    @ApiOperation(value = "设备授权", notes = "设备授权")
    public Wrapper devGrant(@RequestBody crAreaDevice entity) {
        if (icrAreaDeviceService.hasdeviceGrant(entity.getDevicecode()))
            return WrapMapper.error("设备已划分区域");
        entity.setGuid(UUID.randomUUID().toString());
        entity.setCreatetime(new Date());
        icrAreaDeviceService.save(entity);
        return WrapMapper.ok();
    }

    @GetMapping("/areaDevice/selectByCode")
    @ApiOperation(value = "设备授权", notes = "设备授权")
    public Wrapper selectByCode() {

        String param = httpServletRequest.getParameter("param");
        JSONObject _json = new JSONObject();
        crAreaDeviceDto entityDto = (crAreaDeviceDto) _json.toJavaObject(JSON.parseObject(param), crAreaDeviceDto.class);

        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = icrAreaDeviceService.selectByCode(entityDto);
        return WrapMapper.ok(list);
    }
}
